package com.example.howard.xiao7music.model;

import android.util.Log;

import com.example.howard.xiao7music.bean.PlayBean;
import com.example.howard.xiao7music.bean.SingerSongBean;
import com.example.howard.xiao7music.bean.SongBySearchBean;
import com.example.howard.xiao7music.bean.SongDownloadBean;
import com.example.howard.xiao7music.bean.SongRankingBean;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Howard on 2016/9/24.
 */
public class GetRemoteSource {

    private GetRemoteSourceClass getRemoteSourceClass=new GetRemoteSourceClass();
    private SongRankingBean songRankingBean;
    private SingerSongBean singerSongBean;
    private PlayBean playBean;
    private SongDownloadBean songDownloadBean;
    private String url;
    private callback callback;


    public GetRemoteSource(callback callback){
        this.callback=callback;
    }



    public void getSongRanking(int num){

        Call<SongRankingBean> call=getRemoteSourceClass.getSongRanking(num);


        call.enqueue(new Callback<SongRankingBean>() {

            @Override
            public void onResponse(Call<SongRankingBean> call, Response<SongRankingBean> response) {
//                Log.e("GetRemote","succese");
                SongRankingBean bean = response.body();
//                Log.e("GetRemote",bean.toString());
                callback.setRankingSongBean(bean);
            }
            @Override
            public void onFailure(Call<SongRankingBean> call, Throwable t) {
                t.printStackTrace();
                Log.e("GetRemote","failed");
            }
        });
        Log.e("GetRemote","over");

    }


    public void getSingerSong (String singerid){

        Call<SingerSongBean> call=getRemoteSourceClass.getSingerSong(singerid);
        call.enqueue(new retrofit2.Callback<SingerSongBean>() {
            @Override
            public void onResponse(Call<SingerSongBean> call, Response<SingerSongBean> response) {
                SingerSongBean bean=response.body();
//                singerSongBean=bean;
                Log.e("Source",bean.toString());
                callback.setSingerSongBean(bean);
            }

            @Override
            public void onFailure(Call<SingerSongBean> call, Throwable t) {
            }
        });

    }

    public void getSingerSongSearchbySinger(String query){
    }

    public void getSongSearchbySong(String query){
    }


    public void getPlay(String songid){

        Call<PlayBean> call=getRemoteSourceClass.getPlay(songid);

        call.enqueue(new Callback<PlayBean>() {
            @Override
            public void onResponse(Call<PlayBean> call, Response<PlayBean> response) {
                Log.e("serviceplay","true");
                PlayBean bean=response.body();
                playBean=bean;
//                callback.setPlaySongBean(bean);
                getPlayBeanURL(bean);
            }

            @Override
            public void onFailure(Call<PlayBean> call, Throwable t) {
                Log.e("serviceplay","faluse");
            }
        });


    }

    private void getPlayBeanURL(PlayBean playBean){
        url=playBean.getBitrate().getShow_link();
        if (url.equals("")){
            url=playBean.getBitrate().getFile_link();
        }
        callback.setPlaySongURL(url);
    }

    public void getdownloadsong(long songid){

        Call<SongDownloadBean> call=getRemoteSourceClass.getdownloadsong(songid);

        call.enqueue(new Callback<SongDownloadBean>() {

            @Override
            public void onResponse(Call<SongDownloadBean> call, Response<SongDownloadBean> response) {
                SongDownloadBean bean = response.body();
//                songDownloadBean=bean;
                callback.setDownloadSongBean(bean);
            }

            @Override
            public void onFailure(Call<SongDownloadBean> call, Throwable t) {

            }
        });
    }


    public interface callback{
        void setRankingSongBean(SongRankingBean bean);
        void setSingerSongBean(SingerSongBean bean);
        void setPlaySongURL(String url);
        void setDownloadSongBean(SongDownloadBean bean);
    }


}
